package com.motorola.accessories;

import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.preference.Preference;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AccessoriesEntry extends Preference {
    public static final int NO_STATUS = 0;
    public static final int IN_PROGRESS = 1;
    public static final int DONE = 2;
    private boolean mNeedUpdate = false;
    private boolean mDeleteScreen = false;
    private int mStatus = NO_STATUS;
    private int mId = 0;

    private SoapRequestTask mTask;

    private CheckBox mCheck = null;
    private ProgressBar mProgress = null;
    private ImageView mHasUpdate = null;
    private ImageView mNoUpdate = null;

    private UpdateAccessoriesInfo mUpdateAccessoriesInfo;


    public AccessoriesEntry(Context context, UpdateAccessoriesInfo updateAccessoriesInfo, int id) {
        super(context);
        mUpdateAccessoriesInfo = updateAccessoriesInfo;
        mId = id;
    }
    protected View onCreateView(ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View view = inflater.inflate(R.layout.accessories_entry, null );
        TextView title = (TextView)view.findViewById(R.id.title);
        title.setText("sssf");
        TextView summary = (TextView)view.findViewById(R.id.summary);
        summary.setText("fffs");
        mCheck = (CheckBox)view.findViewById( R.id.check );
        mProgress = (ProgressBar) view.findViewById( R.id.progress );
        mHasUpdate = (ImageView)view.findViewById( R.id.has_update );
        mNoUpdate = (ImageView)view.findViewById( R.id.no_update );
        mHasUpdate.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                Intent intent = new Intent(getContext(),NotificationDialog.class);
                getContext().startActivity(intent);
            }
        });
        //summary.setText(getSummary());
        return view;
    }
    protected void onBindView(View view) {
        if (mDeleteScreen) {
            view.setEnabled(true);
            mProgress.setVisibility( View.GONE );
            mHasUpdate.setVisibility( View.GONE );
            mNoUpdate.setVisibility( View.GONE );
            mCheck.setVisibility(View.VISIBLE);
        } else {
            mCheck.setChecked(false);
            view.setEnabled(false);
        switch ( mStatus ) {
            case NO_STATUS:
                mProgress.setVisibility( View.GONE );
                mHasUpdate.setVisibility( View.GONE );
                mNoUpdate.setVisibility( View.GONE );
                mCheck.setVisibility(View.GONE);
                break;
            case IN_PROGRESS:
                mHasUpdate.setVisibility( View.GONE );
                mNoUpdate.setVisibility( View.GONE );
                mCheck.setVisibility(View.GONE);
                mProgress.setVisibility( View.VISIBLE );
                break;
            case DONE:
                mProgress.setVisibility( View.GONE );
                mCheck.setVisibility(View.GONE);
                if(mNeedUpdate) {
                    view.setEnabled(true);
                    mHasUpdate.setVisibility( View.VISIBLE );
                    mHasUpdate.setFocusable(true);
                    //mHasUpdate.setClickable(true);
                } else {
                    mNoUpdate.setVisibility( View.VISIBLE );
                }
                break;
            default:
                break;
        }
        }
    }
    public void setStatus(int status) {
        mStatus = status;
    }
    public int getStatus() {
        return mStatus;
    }
    public void setNeedUpdate(boolean needUpdate) {
        mNeedUpdate = needUpdate;
    }
    public void setDeleteScreen(boolean deleteScreen) {
        mDeleteScreen = deleteScreen;
    }
    public boolean isChecked(){
        if (mCheck != null) {
            return mCheck.isChecked();
        }
        return true;
    }
    public int getId(){
        return mId;
    }
    public void requestInfo () {
        mTask = new SoapRequestTask();
        mTask.execute();
    }
    public void setDeleted(){
        if (mTask != null) {
            mTask.cancel(true);
        }
        new deleteTask().execute();
    }
    private class deleteTask extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return null;
        }
        protected void onPostExecute(Void result) {
            mUpdateAccessoriesInfo.updateNextInfo();
        }
    }

    private class SoapRequestTask extends AsyncTask<Void, Void, Boolean> {
        SoapRequest mSoapRequest = new SoapRequest();
        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                mSoapRequest.request();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mStatus = DONE;
            mNeedUpdate = true;
            return null;
        }

        protected void onPostExecute(Boolean result) {
            mUpdateAccessoriesInfo.updateNextInfo();
        }

    }

    public AccessoriesEntry clone() {
        AccessoriesEntry entry = new AccessoriesEntry(getContext(), mUpdateAccessoriesInfo, mId);
        entry.mCheck = mCheck;
        return entry;
    }
}
